<?php

namespace app\site\controller;

use app\common\model\UserMemberAccountModel;
use app\common\model\UserMemberModel;
use app\common\model\UserModel;
use think\Db;
use think\Request;

class UserMemberAccountController extends BaseController
{
    private $userMemberAccountModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userMemberAccountModel = new UserMemberAccountModel();
    }

    /**
     * 用户流水记录
     * @return mixed
     */
    public function index()
    {
        $map              = [];
        $query            = [];
        $map['site_id']   = $this->getSiteId();
        $query['site_id'] = $this->getSiteId();
        $searchKeyEvent   = $this->request->param('searchKeyEvent');
        $searchText       = $this->request->param('searchText');
        if ($this->request->has('myaccount')) { //只显示固定用户
            $map['member_username'] = $searchText;
            $query['searchText']    = $searchText;
            $userModel = new UserModel();
            $uid = $userModel->where('username',$searchText)->value('uid');
            if($uid>0){
                $userMemberModel = new UserMemberModel();
                $userMemberInfo = $userMemberModel->where('uid',$uid)->find();
                $this->assign('userMemberInfo',$userMemberInfo);
            }
        } else {
            if (!empty($searchText)) {
                $map['member_username'] = ['like', $searchText . '%'];
                $query['searchText']    = $searchText;
            }
        }
        if ($searchKeyEvent>0) {
            $map['change_type']      = $searchKeyEvent;
            $query['searchKeyEvent'] = $searchKeyEvent;
        }
        $changeType = $this->userMemberAccountModel->setChangeType();
        $list       = $this->userMemberAccountModel->where($map)->order('change_time asc')->paginate(10, false, ['query' => $query,]);
        //echo $this->userMemberAccountModel->getLastSql();
        $this->assign('changeType', $changeType);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 资金变动
     */
    public function changeData()
    {
        if ($this->request->isAjax()) {
            $uid = $this->request->param('uid/d');
            $username = $this->request->param('username');
            $changeDesc = $this->request->param('change_desc');
            $userMoney = $this->request->param('user_money/a');
            $payPoints = $this->request->param('pay_points/a');
            if(abs($userMoney['value'])>0){ //金额
                $account['user_money'] = $userMoney['value']*$userMoney['plus'];
            }
            if(abs($payPoints['value'])>0){ //积分
                $account['pay_points'] = $payPoints['value']*$payPoints['plus'];
            }
            $result = $this->userMemberAccountModel->changeAccount($this->getSiteId(), $this->getUid(), $uid, 0, $account, 99, $changeDesc);
            if($result===false){
                $this->error($this->userMemberAccountModel->getError());
            }
            $this->success('调节帐户成功！',url('index',['myaccount'=>1,'searchText'=>$username]));
        }
        $memberUsername = $this->request->param('member_username');
        $userInfo = Db::table('user')->where('username', $memberUsername)->find();
        $memberInfo = Db::table('user_member')->where('uid',$userInfo['uid'])->find();
        $info = array_merge($userInfo,$memberInfo);
        $this->assign('info',$info);
        $this->assign('myaccount',1);
        return $this->fetch();
    }

}
